package com.thread.learn.thread7;

/**
 * 保证可见性的前提
 * 多个线程拿到的是同一把锁
 * 
 * @author feelj
 *
 */
public class Demo2 {
	
	public volatile boolean run=false;
	
	public static void main(String[] args) {
		
		Demo2 demo2=new Demo2();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i=0; i<10;i++) {
					System.out.println("执行了第"+i+" 次");
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				demo2.run=true;
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (!demo2.run) {
					
				}
				System.out.println("线程二执行");
			}
		}).start();
		
	}
	

}
